<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.mail.class.php');

class MailBase {
	var $from_name = '';
	var $from_email_address = '';
	var $to_name = '';
	var $to_email_address = '';
	
	var $email_subject = '';
	var $email_text = '';
	var $subject = '';
	var $content = '';
	
	var $_from_uid = '';
	var $_to_uid = '';
	
	var $_error = 0;
	var $_errmsg = '';
	
	var $_use_html = CONFIG_EMAIL_USE_HTML;
	
	var $mailqueue = false;
	var $mailhistory = false;
	//queue, immediate
	var $mailsendtype = MAIL_SEND_TYPE;
	
	//
	var $filter = '';
	
	//constructor
	function MailBase() {
		global $SETTINGS;
		
		$this->from_name = $SETTINGS['bs_emailname'];
		$this->from_email_address = $SETTINGS['bs_emailaddress'];
		
		$this->to_name = $SETTINGS['bs_emailname'];
		$this->to_email_address = $SETTINGS['bs_emailaddress'];
	}
	
	//methods
	function setMailFrom($from_email_address, $from_name = '') {
		$this->from_email_address = $from_email_address;
		$this->from_name = $from_name;
	}
	
	function setMailTo($to_email_address, $to_name = '') {
		$this->to_email_address = $to_email_address;
		$this->to_name = $to_name;
	}
	
	function setMailContent($email_subject, $email_text = '') {
		$this->email_subject = $email_subject;
		$this->email_text = $email_text;
	}
	
	//
	function addContent($content_wrap) {
		$this->content_wrap = $content_wrap;
	}
	
	function appendContent($content_wrap) {
		if( is_array($content_wrap) ) {
			foreach($content_wrap as $k => $v) {
				$this->content_wrap[$k] = $v;
			}
		}
	}
	
	function setContent($content) {
		$this->content = $content;
	}
	
	function getContent() {
		return $this->content;
	}
	
	//
	function write() {
		$this->_check();
		if( $this->_error == 1 ) {
			return $this->_errmsg;
		}
		
		//
		$from_name = $this->from_name;
		$from_email_address = $this->from_email_address;
		$to_name = $this->to_name;
		$to_email_address = $this->to_email_address;
		
		//
		$email_subject = $this->email_subject;
		$email_text = $this->email_text;
		
		//
		if( substr($email_subject, 0, 1) == ':' ) {
			$email_subject = _lang(substr($email_subject, 1), 'mail');
		}
		if( substr($email_text, 0, 1) == ':' ) {
			$email_text = _lang(substr($email_text, 1), 'mail');
		}
		
		//
		$filename = CONFIG_PATH . CONFIG_DIR_CACHE . 'order_mail_customer_' . time() . '.txt';
		$fp = fopen($filename, "w+");
		fwrite($fp, $email_subject);
		fwrite($fp, $email_text);
		fclose($fp);
		
		return true;
	}
	
	//
	function send($to_name, $to_email_address, $email_subject, $email_text, $from_name = '', $from_email_address = '') {
		if( $from_name == '' ) {
			$from_name = $this->from_name;
		}
		if( $from_email_address == '' ) {
			$from_email_address = $this->from_email_address;
		}
		//
		require_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'lib.mime.class.php');
		require_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'lib.email.class.php');
		/*
		if( defined('CONFIG_EMAIL_CHARSET') ) {
			$emailcharset = CONFIG_EMAIL_CHARSET;
			$email_subject = iconv("UTF-8", $emailcharset, $email_subject);
			$to_name = iconv("UTF-8", $emailcharset, $to_name);
			$from_name = iconv("UTF-8", $emailcharset, $from_name);
			$email_text = iconv("UTF-8", $emailcharset, $email_text);
		}
		*/
		// Instantiate a new mail object
		$message = new LibEmail(array('X-Mailer: windows Mailer'));
		
		// Build the text version
		$text = strip_tags($email_text);
		// html
		if( $this->_use_html == 'true' ) {
			$message->add_html($email_text, $text);
		} else {
			$message->add_text($text);
		}
		// Send message
		$message->build_message();
		
		return $message->send($to_name, $to_email_address, $from_name, $from_email_address, $email_subject);
	}
	
	function sendmail($to_name, $to_email_address, $email_subject, $email_text, $from_name = '', $from_email_address = '') {
		//
		if( $to_name != '' && $to_email_address != '' ) {
			$this->to_name = $to_name;
			$this->to_email_address = $to_email_address;
			if( $from_name != '' ) {
				$this->from_name = $from_name;
			}
			if( $from_email_address != '' ) {
				$this->from_email_address = $from_email_address;
			}
			$this->email_subject = $email_subject;
			$this->email_text = $email_text;
		}
		//
		$this->_check();
		if( $this->_error == 1 ) {
			return $this->_errmsg;
		}
		
		//
		$from_name = $this->from_name;
		$from_email_address = $this->from_email_address;
		$to_name = $this->to_name;
		$to_email_address = $this->to_email_address;
		
		//
		$email_subject = $this->email_subject;
		$email_text = $this->email_text;
		
		//
		if( substr($email_subject, 0, 1) == ':' ) {
			$email_subject = _lang(substr($email_subject, 1), 'mail');
		}
		if( substr($email_text, 0, 1) == ':' ) {
			$email_text = _lang(substr($email_text, 1), 'mail');
		}
		
		//filter
		$email_subject = $this->_do_filter($email_subject);
		$email_text = $this->_do_filter($email_text);
		
		//
		if( $this->mailsendtype == MAIL_SEND_QUEUE ) {
			//
			require_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'mail.queue.class.php');
			$mqObj = new MailQueueBase();
			
			$data_array = array(
					'from_uid' => $this->_from_uid,
					'from_name' => $this->from_name,
					'from_email_address' => $this->from_email_address,
					'to_uid' => $this->_to_uid,
					'to_name' => $this->to_name,
					'to_email_address' => $this->to_email_address,
					'email_subject' => $email_subject,
					'email_text' => $email_text,
						);
			$mqObj->addMQ($data_array);
			
			return true;
		}
		
		//
		require_once(CONFIG_PATH . CONFIG_DIR_LIB . "phpmailer/language/phpmailer.lang-en.php");
		require_once(CONFIG_PATH . CONFIG_DIR_LIB . "phpmailer/class.phpmailer.php");
		
		//
		$mail = new PHPMailer();
		$mail->SetLanguage("en", CONFIG_PATH . CONFIG_DIR_LIB . "/phpmailer/language/");
		
		$mail->CharSet = "UTF-8";
		$mail->Encoding = "base64";
		/*
		$mail->IsSMTP();                                      // set mailer to use SMTP
		$mail->Host = "smtp1.example.com;smtp2.example.com";  // specify main and backup server
		$mail->SMTPAuth = false;     // turn on SMTP authentication
		$mail->Username = "jswan";  // SMTP username
		$mail->Password = "secret"; // SMTP password
		*/
		$mail->From = $from_email_address;
		$mail->FromName = $from_name;
		$mail->AddAddress($to_email_address, $to_name);
		//$mail->AddReplyTo($from_email_address, $from_name);
		
		//$mail->WordWrap = 100;                                // set word wrap to 50 characters
		//$mail->AddAttachment("attachment.jpg");         		// add attachments
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
		$mail->IsHTML(true);                                  	// set email format to HTML
		
		$mail->Subject = _setting('bs_emailsubjectprefix') . $email_subject;
		$mail->Body    = $email_text;
		
		if(!$mail->Send())
		{
			//
			if( $this->mailqueue == false ) {
				return false;
			}
			//
			require_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'mail.queue.class.php');
			$mqObj = new MailQueueBase();
			
			$data_array = array(
					'from_uid' => $this->_from_uid,
					'from_name' => $this->from_name,
					'from_email_address' => $this->from_email_address,
					'to_uid' => $this->_to_uid,
					'to_name' => $this->to_name,
					'to_email_address' => $this->to_email_address,
					'email_subject' => $email_subject,
					'email_text' => $email_text,
						);
			$mqObj->addMQ($data_array);
			//
			return false;
		} else {
			//
			if( $this->mailhistory == false ) {
				return true;
			}
			//
			require_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'mail.history.class.php');
			$mhObj = new MailHistoryBase();
			
			$data_array = array(
					'from_uid' => $this->_from_uid,
					'from_name' => $this->from_name,
					'from_email_address' => $this->from_email_address,
					'to_uid' => $this->_to_uid,
					'to_name' => $this->to_name,
					'to_email_address' => $this->to_email_address,
					'email_subject' => $email_subject,
					'email_text' => $email_text,
						);
			$mhObj->addMH($data_array);
			//
			return true;
		}
	}
	
	function sendToSystem($email_subject, $email_text) {
		$to_name = $this->to_name;
		$to_email_address = $this->to_email_address;
		$from_name = '';
		$from_email_address = '';
		
		return $this->sendmail($to_name, $to_email_address, $email_subject, $email_text, $from_name, $from_email_address);
	}
	
	//
	function _check() {
		if( $this->email_subject == '' ) {
			$this->email_subject = $this->subject;
		}
		if( $this->email_text == '' ) {
			$this->email_text = $this->content;
		}
		if( $this->to_email_address == '' ) {
			$this->_error = 1;
		} elseif( util_validate_email($this->to_email_address) == false ) {
			$this->_error = 1;
		}
	}
	
	function _do_filter($content) {
		if( is_array($this->filter) && sizeof($this->filter) > 0 ) {
			$content = str_replace(array_keys($this->filter), array_values($this->filter), $content);
		}
		
		return $content;
	}
}
//
?>